//
//  SISSContractAlternativeView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/29.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractAlternativeView.h"

@interface SISSContractAlternativeView ()

/// checkOneButton
@property (weak, nonatomic) UIButton *checkOneButton;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkTwoButton;
/// Label
@property (weak, nonatomic) UILabel *label;

/* bottomLine */
@property (weak, nonatomic) UIView *middleLine;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

/// %label
@property (weak, nonatomic) UILabel *oneUnitLabel;
/// 元label
@property (weak, nonatomic) UILabel *twoUnitLabel;

@end
@implementation SISSContractAlternativeView



- (void)setFirstTitle:(NSString *)firstTitle {
    _firstTitle = firstTitle;
    [self.checkOneButton setTitle:firstTitle forState:UIControlStateNormal];
}

- (void)setSecondTitle:(NSString *)secondTitle {
    _secondTitle = secondTitle;
    [self.checkTwoButton setTitle:secondTitle forState:UIControlStateNormal];
}

- (void)setCheckedIndex:(NSInteger)checkedIndex {
    _checkedIndex = checkedIndex;
    if (checkedIndex == 1) {
        self.checkOneButton.selected = YES;
        self.checkedButton = self.checkOneButton;
        self.oneTextField.userInteractionEnabled = YES;
        self.twoTextField.userInteractionEnabled = NO;
    } else if (checkedIndex == 2) {
        self.checkedButton = self.checkTwoButton;
        self.checkTwoButton.selected = YES;
        self.twoTextField.userInteractionEnabled = NO;
        self.oneTextField.userInteractionEnabled = NO;
    }
}


#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    self.checkedButton.selected = NO;
    self.checkedButton = button;
    self.checkedButton.selected = !button.selected;
    
    if (button.tag == 100) {
//        self.model.isChecked = button.isSelected;
        self.oneTextField.userInteractionEnabled = button.isSelected;
        self.twoTextField.userInteractionEnabled = NO;
    } else {
//        self.model.isCheckedTwo = button.isSelected;
        self.twoTextField.userInteractionEnabled = button.isSelected;
        self.oneTextField.userInteractionEnabled = NO;
    }
    
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"mainCheckSelected"] forState:UIControlStateSelected];
    [button setTitle:@"checkTitle" forState:UIControlStateNormal];
    [button setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 100;
    [self addSubview:button];
    self.checkOneButton = button;
    
    
    //    UILabel *label = [[UILabel alloc] init];
    //    label.numberOfLines = 0;
    //    label.textColor = MAIN_TEXT_COLOR;
    //    label.font = [UIFont systemFontOfSize:16];
    //    label.textAlignment = NSTextAlignmentRight;
    //    [self addSubview:label];
    //    self.label = label;
    
    UITextField *textField = [[UITextField alloc] init];
    
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    textField.userInteractionEnabled = NO;
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    textField.placeholder = @"请输入数字";
    [self addSubview:textField];
    self.oneTextField = textField;
    
    UILabel *oneUnitLabel = [[UILabel alloc] init];
    oneUnitLabel.textColor = MAIN_TEXT_COLOR;
    oneUnitLabel.text = @"%";
    oneUnitLabel.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:oneUnitLabel];
    self.oneUnitLabel = oneUnitLabel;
    
    UIView *middleLine = [[UIView alloc] init];
    middleLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:middleLine];
    self.middleLine = middleLine;
    
    UIButton *checkTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkTwoButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [checkTwoButton setImage:[UIImage imageNamed:@"mainCheckSelected"] forState:UIControlStateSelected];
    [checkTwoButton setTitle:@"checkTitle" forState:UIControlStateNormal];
    [checkTwoButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [checkTwoButton addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    checkTwoButton.tag = 101;
    [self addSubview:checkTwoButton];
    self.checkTwoButton = checkTwoButton;
    
    
    UITextField *twoTextField = [[UITextField alloc] init];
    twoTextField.userInteractionEnabled = NO;
    twoTextField.textColor = MAIN_TEXT_COLOR;
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    twoTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    twoTextField.placeholder = @"请输入金额";
    [self addSubview:twoTextField];
    self.twoTextField = twoTextField;
    
    UILabel *twoUnitLabel = [[UILabel alloc] init];
    twoUnitLabel.textColor = MAIN_TEXT_COLOR;
    twoUnitLabel.text = @"元";
    twoUnitLabel.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:twoUnitLabel];
    self.twoUnitLabel = twoUnitLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.checkOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf).offset(1.5 * margin);
    }];
    
    //    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(weakSelf.checkOneButton.mas_right).offset(margin);
    //        make.top.equalTo(weakself).offset(1.5 * margin);
    //    }];
    
    [self.oneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_centerX);
        make.centerY.equalTo(weakSelf.checkOneButton);
    }];
    
    [self.oneUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.oneTextField.mas_right).offset(margin);
        make.centerY.equalTo(weakSelf.checkOneButton);
    }];
    
    [self.middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.checkOneButton.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(1);
    }];
    
    [self.checkTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.top.equalTo(weakSelf.middleLine.mas_bottom).offset(margin);
    }];
    
    [self.twoTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_centerX);
        make.centerY.equalTo(weakSelf.checkTwoButton);
    }];
    
    [self.twoUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.twoTextField.mas_right).offset(margin);
        make.centerY.equalTo(weakSelf.checkTwoButton);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.checkTwoButton.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf);
        make.height.mas_equalTo(1);
    }];
}



@end
